﻿layui
		.use(
				[ 'form', 'layer', 'jquery', 'laypage', 'table' ],
				function() {
					var form = layui.form, table = layui.table, layer = parent.layer === undefined ? layui.layer
							: parent.layer, laypage = layui.laypage, $ = layui.jquery;

					// 数据表格
					table.render({
						id : 'classificationList',
						elem : '#classificationList',
						url : ctx + '/classification/getClassificationList' // 数据接口
						,
						cellMinWidth : 80,
						limit : 10// 每页默认数
						,
						limits : [ 10, 20, 30, 40 ],
						cols : [ [ // 表头
						{
							field : 'classification_id',
							title : '分类ID',
							sort : true,
							align : 'center',
							width : 120
						}, {
							field : 'second_classification',
							title : '二级分类名称',
							align : 'center'
						}, {
							field : 'product_classification',
							title : '分类名称',
							align : 'center'
						}, {
							field : 'classification_display',
							title : '分类状态',
							templet: '#switchTpl',
							align : 'center'
						}, {
							field : 'classification_images',
							title : '分类图片',
							templet : "#imgtmp",
							align : 'center'
						}, {
							title : '操作',
							toolbar : '#barEdit',
							align : 'center'
						} ] ],
						page : true
					// 开启分页
					});
					 //监听性别操作
					
					  form.on('switch(sexDemo)', function(obj){
						  var classification_display = this.checked ? 1 : 0;
							var classification_id = this.value;
							$.ajax({
								url : ctx + '/classification/updateClassificationStatusById',
								type : "get",
								data:{
									classification_display:classification_display,
									classification_id:classification_id
								},
								success : function(d) {
									if (d.code == 0) {
										layer.msg("更新商品分类状态成功！");
									} else {
										layer.msg("更新失败！", {
											icon : 5
										});
									}
								}
							})
					  });
					// 监听工具条
					table.on('tool(classificationList)', function(obj) {
						var data = obj.data;
						if (obj.event === 'del') {
							if (data.roleName == '超级管理员') {
								layer.msg("不允许操作此角色！", {
									icon : 5
								});
								return;
							}
							layer.confirm('真的删除行么', function(index) {
								$.ajax({
									url : ctx
											+ '/classification/deleteClassificationById/?classification_id='
											+ data.classification_id,
									type : "get",
									success : function(d) {
										if (d.code == 0) {
											// obj.del();
											table.reload('classificationList', {})
										} else {
											layer.msg(d.msg, {
												icon : 5
											});
										}
									}
								})
								layer.close(index);
							});
						} else if (obj.event === 'edit') {
							layer.open({
								type : 2,
								title : "编辑产品",
								area : [ '380px', '600px' ],
								content : ctx + "/classification/editClassification/"
										+ data.classification_id,
							})
						}
					});
					$(".search_btn").click(function() {
						var type = $(this).data('type');
						active[type] ? active[type].call(this) : '';
					})
					active = {
						search : function() {
							var second_classification = $('#second_classification');
							var product_classification = $('#product_classification');
							var classification_display = $('#classification_display');

							table.reload('classificationList', {
								page : {
									curr : 1
								},
								where : {
									second_classification : second_classification.val(),
									product_classification : product_classification.val(),
									classification_display : classification_display.val(),
								}
							});
						}
					};

					// 添加角色
					$(".classificationAdd_btn").click(function() {
						var index = layui.layer.open({
							title : "添加产品",
							type : 2,
							content : ctx + "/classification/addClassification",
							area : [ '380px', '550px' ]
						})
						// 改变窗口大小时，重置弹窗的高度，防止超出可视区域（如F12调出debug的操作）
						$(window).resize(function() {
							layui.layer.full(index);
						})
					})

				})
